package com.itany.book.service.impl;

import com.itany.book.service.IBookService;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tyh on 2018/9/23.
 */
@Service
@com.alibaba.dubbo.config.annotation.Service
public class BookServiceImpl implements IBookService{


    @Autowired
     private HttpSolrClient solrClient;


    @Override
    public void addDoc() {
        try {
            // mapper ---> select book
            for(int i=0;i<10;i++){
                SolrInputDocument doc = new SolrInputDocument();
                doc.addField("id",i+1);
                doc.addField("username","风云"+i);
                solrClient.add(doc);
            }
            solrClient.commit();
        }catch (Exception e){
            e.printStackTrace();
        }


    }

    @Override
    public List<Map<String, String>> findDoc() {
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        try {

            String q="username:风云";
            SolrQuery solrQuery = new SolrQuery();
            solrQuery.setQuery(q);
            /*solrQuery.setStart("(pageNo-1)*pagesize");
            solrQuery.setRows("pagesize");*/
            solrQuery.setHighlight(true);
            solrQuery.setHighlightSimplePre("<font color='red'>");
            solrQuery.setHighlightSimplePost("</font>");
            solrQuery.setParam("hl.fl","*");
            QueryResponse response = solrClient.query(solrQuery);

            SolrDocumentList results = response.getResults();
            //select count(1)
            System.out.println("当前一共"+results.getNumFound());
            for(int i=0;i<results.size();i++){
                  Map<String,String> vo = new HashMap<String,String>();
                  SolrDocument sdoc = results.get(i);
                  String id = (String)sdoc.get("id");
                  Map<String,List<String>> map=response.getHighlighting().get(id);
                  List<String> usernamelist = map.get("username");
                  if(null!=usernamelist){
                      vo.put("username",usernamelist.get(0));
                  }else{
                      vo.put("username", (String)sdoc.get("username"));
                  }
                  list.add(vo);
            }



        }catch (Exception e){
            e.printStackTrace();
        }


        return list;
    }
}
